﻿using System.Collections.Generic;

namespace Ipmce.RemoteVoting.Common.Tools.CardReader.ImageParsing
{
	class BiometricInformationGroup : LdsTagElement
	{
		public List<BiometricInformation> BioInfoList { get; set; }

		protected override int GetChildrenSize()
		{
			return base.GetChildrenSize() + 3;
		}

		protected override int ParseChildren(byte[] data, int offset)
		{
			var numInstTag = new ByteArrayWithTag(BioInfoTags.NumberOfInstances, 1);
			offset = numInstTag.Parse(data, offset);

			for (byte i = 1; i <= numInstTag.Value[0]; i++)
			{
				var child = new BiometricInformation(i);
				Children.Add(child);
				BioInfoList.Add(child);
			}

			return base.ParseChildren(data, offset);
		}

		protected override int WriteChildren(byte[] data, int offset)
		{
            
			offset = new ByteArrayWithTag(BioInfoTags.NumberOfInstances,
			                              new[] { (byte)BioInfoList.Count }).Write(data, offset);

			return base.WriteChildren(data, offset);
		}

		public BiometricInformationGroup()
			: base(BioInfoTags.BiometricInformationGroup, true, null, false)
		{
			BioInfoList = new List<BiometricInformation>();
		}

		public BiometricInformationGroup(params BiometricInformation[] bioInfoList)
			: base(BioInfoTags.BiometricInformationGroup, true, null, false)
		{
			BioInfoList = new List<BiometricInformation>();
			foreach (var bioInfo in bioInfoList)
			{
				Children.Add(bioInfo);
				BioInfoList.Add(bioInfo);
			}
		}
	}
}